package com.foreknow.dao.impl;

import com.foreknow.bean.User;
import com.foreknow.dao.UserDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactoy;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public boolean save(User user)throws SQLException {
        String sql = "insert into users(adminname,password) values(?,?)";
        int isRigth = jdbcTemplate.update(sql,user.getAdminName(),user.getPassword());
        return isRigth == 1;
    }

    @Override
    public User join(String adminname, String password)throws SQLException {
        String sql = "select * from users where adminname = ? and password = ? ";
        EntityMapping mapping = mappingFactoy.getmapping(MappingFactoy.USER_MAPPING);
        List<Object> list= jdbcTemplate.query(sql,mapping,adminname,password);
        if (list.size()>0){
            User user = (User) list.get(0);
            return user;
        }
        return null;
    }

//    public static void main(String[] args) {
//        UserDaoImpl userDao = new UserDaoImpl();
//        User u1 = new User();
//        u1.setAdminName("fender");
//        u1.setPassword("456789");
//        boolean i = userDao.save(u1);
//        if (i == true){
//            System.out.println("ok");
//        }else {
//            System.out.println("no");
//        }
//        User u2 = new User();
//        u2 = userDao.join("admin","admin");
//        System.out.println(u2.getAdminId()+" "+u2.getAdminName()+" "+u2.getPassword());
//    }
}
